<script setup lang="ts">
// import { dayjs } from 'element-plus'
// import { computed } from 'vue'
import { spDrugBatchListApi, spDrugInOutOrderSeeApi, spDrugListApi } from '@/api/cy/DrugGoTo'
import { PropType, ref } from 'vue'

import EarconRefer from './EarconRefer.vue'

const prop = defineProps({
  currentRow: {
    type: Object,
    default: () => null
  },
  detailSchema: {
    type: Array as PropType<DescriptionsSchema[]>,
    default: () => []
  },
  id: {
    type: String,
    default: null
  }
})
console.log(prop)
// const isExpand = ref(true)

const tableData = ref<any>([])

// (修改) 调用查看接口获取信息
const getMedicalOrderSee = async () => {
  console.log(prop.id)
  if (prop.id) {
    const res = await spDrugInOutOrderSeeApi({ id: prop.id })
      .catch(() => {})
      .finally(() => {})
    if (res) {
      tableData.value = await res.data.detailed

      await tableData.value
      console.log(await tableData.value, '222')
    }
  } else {
    tableData.value = []
    // await methods.setValues(data.value)
  }
}
const init = async () => {
  // 调用查看接口查询数据
  // await onCustomer(data.value.pasture_org_id)
  await getMedicalOrderSee()
  // await churu(data.value.type)
}
init()

const changeDialog = (index) => {
  if (!tableData.value[index].batch) {
    console.log(index, !tableData.value[index].batch, 'dsibf')
    tableData.value[index].batch = []
  }
  tableData.value[index].batch.push({
    name: '',
    batch_id: '',
    start_date: '',
    end_date: '',
    amount: '',
    total_actual_number: 0
  })
}

const resetPasswords = () => {
  tableData.value.push({
    drug_code: '',
    amount: '',
    amount_z: '',
    batch: []
  })
}

const addDelete = (row, index) => {
  console.log(row, index)
  tableData.value.splice(index, 1)
}

const addDeleteming = (row, props, index) => {
  console.log(row, props, index, tableData.value[props.$index], 'ming')
  tableData.value[props.$index].batch.splice(index, 1)
}

const getGuigeListApi = async (new_data) => {
  // let formData: any = await methodsForm.getFormData()
  console.log(new_data, tableData.value, 'rrr1111111')
  //   if (!!formData.pasture_org_id) {
  // console.log('formData', !formData.customer_id, new_data, adminFaEarconListApi)
  const res = await spDrugBatchListApi({
    ...new_data,
    pasture_org_id: prop.currentRow.pasture_org_id,
    drug_id: Drug.value
  })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    return res
  }
}

// 药品档案Api
const getDanganListApi = async (new_data) => {
  // if (!!formData.pasture_org_id) {
  // console.log('formData', !formData.customer_id, new_data, adminFaEarconListApi)
  const res = await spDrugListApi({ ...new_data, pasture_org_id: prop.currentRow.pasture_org_id })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    return res
  }
}

function handleBlur(props, scope) {
  console.log('就这', props, scope)
  let num = ref(0)
  for (let index = 0; index < props.row.batch.length; index++) {
    // console.log(props.row.batch[index].actual_number)
    num.value += Number(props.row.batch[index].actual_number)
  }
  console.log(num.value)
  props.row.total_actual_number = num.value
}

// let code = ref()
// const dac = (scope, props) => {
//   if (props.row.batch[scope.$index].batch_code) {
//     props.row.batch[scope.$index].batch_code = props.row.batch[scope.$index].batch_code
//   } else {
//     props.row.batch[scope.$index].batch_code = code.value
//   }
// }

// const da = (e) => {
//   code.value = e.target.value
//   // console.log(e.target.value, 'hhh')
// }

let Drug = ref()
const dian = (b) => {
  Drug.value = tableData.value[b.$index].drug_id
  console.log(b, tableData.value[b.$index].drug_id, '222222222')
}

defineExpose({ tableData, prop })
</script>

<template>
  <el-button type="warning" size="small" @click="resetPasswords()">增加</el-button>
  <el-table :data="tableData" :border="false" style="width: 100%" :default-expand-all="true">
    <el-table-column type="expand">
      <template #default="props">
        <div m="4">
          <el-table :data="props.row.batch" :border="false">
            <el-table-column label="批次明细" prop="name" width="80" />
            <el-table-column label="批次号" prop="batch_code">
              <template #default="scope">
                <EarconRefer
                  v-model="props.row.batch[scope.$index].batch_code"
                  :getListApi="getGuigeListApi"
                  :response="{ count: 'count', data: 'data' }"
                  title="选择批次（双击选择）"
                  nameField="brug_code"
                  relNameField="batch_code"
                  valueField="batch_code"
                  :crudSchemas="[
                    // { field: 'is_selected', label: '是否已选', search: { show: false }, width: 100 },
                    { field: 'batch_code', label: '批次编号', search: { show: false } },
                    { field: 'drug_name', label: '名称', search: { show: false } },
                    { field: 'available_stock', label: '可用库存', search: { show: false } },
                    { field: 'drug_id', label: '药品_id', search: { show: true } }
                  ]"
                  :modelName="props.row.batch[scope.$index].batch_code"
                  :onclick="dian(props)"
                  :disabled="props.row.batch[scope.$index].id ? true : false"
                />
              </template>
            </el-table-column>

            <el-table-column label="生产日期" prop="start_date">
              <template #default="scope">
                <ElDatePicker
                  v-model="props.row.batch[scope.$index].start_date"
                  valueFormat="YYYY-MM-DD"
                  :disabled="props.row.batch[scope.$index].id ? true : false"
                />
              </template>
            </el-table-column>

            <el-table-column label="过期日期" prop="end_date">
              <template #default="scope">
                <ElDatePicker
                  v-model="props.row.batch[scope.$index].end_date"
                  valueFormat="YYYY-MM-DD"
                  :disabled="props.row.batch[scope.$index].id ? true : false"
                />
              </template>
            </el-table-column>
            <el-table-column label="入库数量" prop="actual_number">
              <template #default="scope">
                <el-input
                  v-model="props.row.batch[scope.$index].actual_number"
                  @blur="handleBlur(props, scope)"
                  :disabled="props.row.batch[scope.$index].id ? true : false"
                />
              </template>
            </el-table-column>
            <!-- <el-table-column label="Address" prop="address" />
            <el-table-column label="Zip" prop="zip" /> -->

            <el-table-column label="操作" width="80" fixed="right">
              <template #default="scope">
                <!-- <el-button
                  type="warning"
                  size="small"
                  @click="resetPasswords(scope, props, scope.$index)"
                  >增加子菜单</el-button
                > -->
                <el-button
                  type="danger"
                  size="small"
                  @click="addDeleteming(scope.row, props, scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="药品" width="170" prop="drug_name">
      <template #default="scope">
        <EarconRefer
          v-model="scope.row.drug_id"
          :getListApi="getDanganListApi"
          :response="{ count: 'count', data: 'data' }"
          title="选择档案（双击选择）"
          nameField="name"
          relNameField="name"
          valueField="id"
          :crudSchemas="[
            { field: 'is_selected', label: '是否已选', search: { show: false }, width: 280 },
            { field: 'name', label: '档案名称', search: { show: true } },
            { field: 'pasture_org_name', label: '所属牧场', search: { show: true } }
          ]"
          :modelName="scope.row.drug_name"
        />
      </template>
    </el-table-column>
    <el-table-column label="初始金额" width="100" prop="amount">
      <template #default="scope">
        <el-input v-model="scope.row.amount" />
      </template>
    </el-table-column>

    <el-table-column label="申请数量" width="100" prop="apply_number">
      <template #default="scope">
        <el-input v-model="scope.row.apply_number" />
      </template>
    </el-table-column>
    <el-table-column label="实际数量" prop="total_actual_number">
      <!-- <template #default="scope">
        <el-input v-model="scope.row.total_actual_number" />
      </template> -->
    </el-table-column>
    <!-- <el-table-column label="实际总数量" prop="actual_number" /> -->
    <el-table-column label="操作" width="180" fixed="right">
      <template #default="scope">
        <el-button type="success" size="small" @click="changeDialog(scope.$index)"
          >添加子菜单</el-button
        >

        <!-- <el-button
          type="warning"
          size="small"
          @click="resetPasswords(scope.row, null, scope.$index)"
          >增加</el-button
        > -->
        <el-button type="danger" size="small" @click="addDelete(scope.row, scope.$index)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
</template>
<style scoped></style>
